package cn.psvmc.cxzapi.controller.system;

import cn.psvmc.cxzapi.model.common.DelEntity;
import cn.psvmc.cxzapi.model.common.SearchEntity;
import cn.psvmc.cxzapi.model.system.TMenu;
import cn.psvmc.cxzapi.response.Result;
import cn.psvmc.cxzapi.response.ResultGenerator;
import cn.psvmc.cxzapi.service.system.TMenuService;
import cn.psvmc.cxzapi.util.ToolUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 菜单表相关操作
 * @Author auto
 * @Date 2024-08-11
 */
@RestController
@RequestMapping("/menu")
public class TMenuController {

    @Resource
    private TMenuService tMenuService;

    /**
    * @Author auto 2024-08-11 新增菜单表
    * @Param tMenu 菜单表
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/add")
    public Result add(@RequestBody TMenu tMenu) {
        ToolUtils.validation(new Object[]{tMenu.getMenuname(), "菜单名称", 2, tMenu.getMenutype(), "菜单类型", 1});
        Integer leaf = tMenu.getLeaf();
        if (leaf == null) return ResultGenerator.genFailResult("是否是叶子节点不能为空！");
        if (leaf.equals(1) && ToolUtils.isEmpty(tMenu.getUrl())) return ResultGenerator.genFailResult("地址不能为空！");
        if (leaf.equals(1) && ToolUtils.isEmpty(tMenu.getOperates())) {
            return ResultGenerator.genFailResult("模块操作不能为空！");
        }
        tMenuService.add(tMenu);
        return ResultGenerator.genSuccessResult("保存成功！");
    }

    /**
    * @Author auto 2024-08-11 更新菜单表
    * @Param tMenu 菜单表模型
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/edit")
    public Result edit(@RequestBody TMenu tMenu) {
        ToolUtils.validation(new Object[]{tMenu.getMenuid(), "id", 1, tMenu.getRversion(), "版本", 1});
        Integer leaf = tMenu.getLeaf();
        if (leaf == null) return ResultGenerator.genFailResult("是否是叶子节点不能为空！");
        if (leaf.equals(1) && ToolUtils.isEmpty(tMenu.getUrl())) return ResultGenerator.genFailResult("地址不能为空！");
        if (leaf.equals(1) && ToolUtils.isEmpty(tMenu.getOperates())) {
            return ResultGenerator.genFailResult("模块操作不能为空！");
        }
        tMenuService.edit(tMenu);
        return ResultGenerator.genSuccessResult("更新成功！");
    }

    /**
    * @Author auto 2024-08-11 删除菜单表
    * @Param tMenu
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/delete")
    public Result delete(@RequestBody DelEntity tMenu) {
        Integer id = tMenu.getId();
        Integer rversion = tMenu.getRversion();
        ToolUtils.validation(new Object[]{id, "id", 1, rversion, "版本", 1});
        int delResult = tMenuService.delete(id, rversion);
        if (delResult > 0) {
            return ResultGenerator.genSuccessResult("删除成功！");
        }
        return ResultGenerator.genFailResult("删除失败！");
    }

    /**
    * @Author auto 2024-08-11 查询菜单表列表
    * @Param entity 分页查询通用实体类
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/list")
    public Result list(@RequestBody SearchEntity entity) {
        return ResultGenerator.genSuccessResult("查询成功！", tMenuService.listAll(entity));
    }

    /**
     * @Description 获取父级菜单列表
     * @Date 2024/8/20 16:47
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_parent")
    public Result listParent() {
        return ResultGenerator.genSuccessResult("查询成功！", tMenuService.listParent());
    }
}
